<template>
  <div>
    <el-card>
      <el-form ref="form" :model="form" label-width="80px" class="form">
        <el-form-item label="用户名称">
          <el-input v-model.trim="form.username" />
        </el-form-item>
        <el-form-item label="用户邮箱">
          <el-input v-model.trim="form.email" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.role_id" placeholder="请选择">
            <el-option label="超级管理员" value="超级管理员" />
            <el-option label="管理员" value="管理员" />
            <el-option label="老师" value="老师" />
            <el-option label="学生" value="学生" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="reset">清除</el-button>
          <el-button type="primary" @click="add">+新增用户</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table :data="userList" border style="width: 100%">
        <el-table-column fixed label="序号" type="index" width="100" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="role_id" label="角色" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="status" label="状态" />
        <el-table-column fixed="right" label="操作">
          <template v-slot="{ row }">
            <el-button
              type="text"
              size="small"
              @click="edit(row)"
            >编辑</el-button>
            <el-button type="text" size="small" @click="changeStatus(row)">{{
              row.status === '启用' ? '禁用' : '启用'
            }}</el-button>
            <el-button
              type="text"
              size="small"
              @click="del(row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="pageInfo.page"
        :page-size.sync="pageInfo.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
        :page-sizes="[1, 5, 10, 20]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 对话框 -->
    <changeDialog ref="changeDialog" @getUserList="getUserList" />
  </div>
</template>

<script>
import { getUserList, userStatus, delUser } from '../../api/user.js'
import changeDialog from './components/changeDialog'
export default {
  name: 'UserList',
  components: {
    changeDialog
  },
  data() {
    return {
      userList: [],
      form: {
        username: '',
        email: '',
        role_id: ''
      },
      pageInfo: {
        page: 1,
        limit: 5,
        total: null
      }
    }
  },
  created() {
    this.getUserList()
  },

  methods: {
    async changeStatus(val) {
      await userStatus(val.id)
      this.getUserList()
    },
    async del(val) {
      await delUser(val.id)
      this.$message.success('删除用户成功')
      this.getUserList()
    },
    edit(val) {
      console.log(val)
      this.$refs.changeDialog.form = val
      this.$refs.changeDialog.meta = '编辑用户'
      this.$refs.changeDialog.showDialog = true
    },
    add() {
      this.$refs.changeDialog.meta = '新增用户'
      this.$refs.changeDialog.showDialog = true
    },
    reset() {
      this.getUserList()
      this.form = {}
    },
    async search() {
      await this.getUserList(this.form)
    },
    async getUserList() {
      const res = await getUserList({
        ...this.pageInfo,
        ...this.form
      })
      this.pageInfo.total = res.total
      this.userList = res.data.items.map(v => {
        if (v.role_id === 1) {
          v.role_id = '超级管理员'
        } else if (v.role_id === 2) {
          v.role_id = '管理员'
        } else if (v.role_id === 3) {
          v.role_id = '老师'
        } else if (v.role_id === 4) {
          v.role_id = '学生'
        }
        if (v.status === 1) {
          v.status = '启用'
        } else {
          v.status = '禁用'
        }
        return v
      })
      this.pageInfo.total = res.data.pagination.total
    },
    handleSizeChange() {
      this.getUserList(this.form)
    },
    handleCurrentChange() {
      this.getUserList(this.form)
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep {
  .form.el-form {
    display: flex;
  }

  .el-form-item__label {
    font-weight: normal;
  }
  .el-pagination{
    text-align: center;
    margin-top: 20px;
  }
}
</style>
